package org.lanqiao.netctoss.util;

import java.sql.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;

public class JDBCUtil {
	static Connection conn = null;
	public static Connection getConnection(){
				
		try {
			Context ct = new InitialContext();
			DataSource ds = (DataSource) ct.lookup("java:comp/env/jdbc/mysql");
			conn = ds.getConnection();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {                 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{			
			return conn;
		}
	}
	public static void close(){
		try {
			if(conn!=null) conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	static String db = "mysql";
	public static String getDB(){
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.prop");
		Properties prop = new Properties();
		try {
			prop.load(is);
			db = prop.getProperty("db");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "mysql";
	}

}
